package com.bone.eventsweb.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    //阿里云的域名
    public static final String ALI_DOMAIN ="https://dkx-test.oss-cn-beijing.aliyuncs.com/";
    //生成新的文件名


    public static String uploadImage (MultipartFile file) throws IOException {
        //生成文件名
        String originalFilename = file.getOriginalFilename();  //originalFilename============16256251975051038.png
       //方法1： String ext = originalFilename.split("\\.")[1];  // 1.png    拿到后缀名  png
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");//0da7f3c93a834ea18638aa3723514915 以uuid生成文件名
        //新文件名
        String fileName = uuid+ ext;  //fileNamedc75cd16f556468fbe1af5313f6c3684png
        //地域节点
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t6N7ighKVKpAMMnQKGb";
        String accessKeySecret = "cJhQz2YHGl3cLTnsz2VYmywgWzn2Zz";
        //OOS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);



        ossClient.putObject(
                "dkx-test",//仓库名
                fileName, //文件明
                file.getInputStream() //
        );

        //对象存储使用完毕关掉
        ossClient.shutdown();
        return ALI_DOMAIN+fileName;
    }



}
